#include "mylabel.h"
#include <QMouseEvent>
#include <QDebug>


MyLabel::MyLabel(QWidget *parent) : QLabel(parent)
{
    // 窗口部件跟踪鼠标是否生效
    // 设置为 true, 如果鼠标跟踪生效，如果没有按键被按下，这个窗口部件也会接收鼠标移动事件
    // 默认为 false, 鼠标按键未被按下则鼠标移动事件不生效
    this->setMouseTracking(true);
}
// 鼠标被按下触发
void MyLabel::mousePressEvent(QMouseEvent *ev) {
    int i = ev->x();
    int j = ev->y();


    // 获取鼠标的按键
    if (ev->button() == Qt::LeftButton) {
        qDebug() << "left";
    } else if (ev->button() == Qt::RightButton) {
        qDebug() << "right";
    } else if (ev->button() == Qt::MidButton) {
        qDebug() << "mid";
    }


    /*  字符串格式化, arg里的参数 123 和 mike 分别替换 %1, %2
     *  QString str = QString("abc %1 ^_^ %2").arg(123).arg("mike");
     */
    QString text = QString("<center><h1>Mouse Press: (%1, %2)</h1></center>")
        .arg(i).arg(j);

    this->setText(text);


}

void MyLabel::mouseReleaseEvent(QMouseEvent *ev) {
    QString text2 = QString("<center><h1>鼠标松开: (%1, %2)</h1></center>")
            .arg(ev->x()).arg(ev->y());
   this->setText(text2);
}

void MyLabel::mouseMoveEvent(QMouseEvent *ev) {
    QString text3 = QString("<center><h1>鼠标移动: (%1, %2)</h1></center>")
           .arg(ev->x()).arg(ev->y());
    // this->setText(text3);
}

void MyLabel::enterEvent(QEvent *e) {
    QString enterText = QString("<center><h1>鼠标进入</h1></center>");
    this->setText(enterText);
}


void MyLabel::leaveEvent(QEvent *e) {
    QString leaveText = QString("<center><h1>鼠标离开</h1></center>");
    this->setText(leaveText);
}








